<!DOCTYPE html>
<html lang="zh-CN"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>转让</title>
    <meta charset="UTF-8">
    <title>转让</title>
    <meta name="keywords" content="转让"/>
    <meta name="description" content="转让"/>
    <meta name="viewport" content="width=device-width,initial-scale=1, maximum-scale=1,minimum-scale=1, user-scalable=no">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="format-detection" content="telephone=no"/>
    <meta name="viewport" content="width=device-width,viewport-fit=cover">
    <link th:href="@{/shoppingApi/css/public.css}" rel="stylesheet">
    <link th:href="@{/shoppingApi/css/publicOfuser.css}" rel="stylesheet">
    <link th:href="@{/shoppingApi/css/combtn.css}" rel="stylesheet">
    <link th:href="@{/shoppingApi/css/accsetting.css}" rel="stylesheet">
    <link th:href="@{/shoppingApi/css/weui.min.css}" rel="stylesheet">
    <link th:href="@{/shoppingApi/css/jquery-weui.min.css}" rel="stylesheet">
    <meta name="_csrf" th:content="${_csrf.token}"/>
    <meta name="_csrf_header"  th:content="${_csrf.headerName}"/>
</head>
<body>
    <div class="main accsetting">

        <div class="main_con c323232 withdraw">

            <div class="userinfo title">
                <span>转让</span>
            </div>

            <div class="mt10 pl15 pr15 height44 bgfff flex flex-pj flex-ac item">
                <label for="transferFrom" class="ft16">转出账号</label>
                <div><input id="transferFrom" th:data="${map.paystate}" class="ft16" type="text" readonly th:if="${map!=null&&map.userNo!=null}" th:value="${map.userNo.userNo}" ></div>
                <!--<input type="hidden" name="payInfo" id="payInfo" th:if="${map.userNoPayInfo!=null}" th:value="${map.userNoPayInfo}">-->
            </div>

            <div class="mt1 pl15 pr15 height44 bgfff flex flex-pj flex-ac item">
                <label for="transferTo" class="ft16">转入账号</label>
                <div><input id="transferTo" class="ft16" type="text" ></div>
                <span class="msg" style="color: red"></span>
            </div>

            <div class="mt30 mb10 pl15 tips">
                可转让积分（元）<span id="max" th:if="${map!=null && map.finaceTotal!=null}" th:text="${map.finaceTotal.balance}">0.00</span>
            </div>

            <div class="mt1 pl15 pr15 pt10 pb10 bgfff input">
                <label for="total" class="ft14">转让积分</label>
                <div><input id="total" class="ft18" type="text" placeholder="￥0.00" autocomplete="off"></div>
            </div>

            <div class="pl5 pr15 mt40"><a class="submit  button red bigrounded">下一步</a></div>

        </div>

    </div>
    <div class="weui-mask-custom"></div>
    <div id="password" class="weui-dialog-custom">
        <div class="weui-dialog__hd">
            <strong class="weui-dialog__title">输入密码</strong>
        </div>
        <div class="weui-dialog__bd">
            <div class="passwordarea">
                <ul class="passwordgrid">
                    <li></li><li></li><li></li><li></li><li></li><li></li>
                </ul>
                <input class="passwordinput" id="passwordinput" autocomplete="new-password" type="password" maxlength="6">
            </div>
        </div>
        <div class="weui-dialog__ft">
            <a href="javascript:;" class="weui-dialog__btn default" id="cancel">取消</a>
            <a href="javascript:;" class="weui-dialog__btn primary" id="confirm">确定</a>
        </div>
    </div>
</body>
<script th:src="@{/shoppingApi/js/jquery-3.3.1.min.js}"></script>
<script th:src="@{/shoppingApi/js/jquery-weui.min.js}"></script>
<script>
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return encodeURIComponent(unescape(r[2]));
        return null;
    }
    var f=false;
    $(function () {

        // 若无设置支付密码，则前往设之
        var state=$.trim(getQueryString("state"));
        if(state=="1"){
            $("#transferFrom").attr("data","true");
        }
        var paystate=$.trim($("#transferFrom").attr("data"));
        if (paystate==false) {
        // var  payInfo=$('#payInfo').val();
        // if (payInfo=='0') {
            $.alert("积分转让需设置支付密码，请前往设置！", "提示", function() {
                //点击确认后的回调函数
                var redirect = encodeURIComponent('/wap/openTransfer');
                location.href = '/wap/updatePayPwd?redirect=' + redirect;
            });
        }
        if(navigator.userAgent.indexOf("Html5Plus") > -1){
            $('.userinfo.title').hide();
        }
        $('#transferTo').focus();
        $.toast.prototype.defaults.duration = 1000;
        $('.submit').on('click',function () {
            var from = $('#transferFrom').val();
            var to = $('#transferTo').val();
            var total = $('#total').val();
            var max = Number($('#max').text());

            var u= $('#transferTo').val();
            f=confireUserNo(u);

            if(!f){
                $.toast('请填写正确转入账号', 'text');
                return;
            }
            if (!to) {
                $.toast('请填写转入账号', 'text');
                return;
            }
            if (!total) {
                $.toast('请输入转让积分', 'text');
                return;
            }
            if (Number(total) > max) {
                $.toast('可转让积分不足', 'text');
                return;
            }
            flag=setPayPwd(from);
            if(flag){
                $.confirm("积分转让", '确定转让？', function() {
                    //点击确认后的回调函数
                    // 输入密码
                    $('#password').addClass('weui-dialog--visible').siblings('.weui-mask-custom').addClass('weui-mask--visible');
                    $('#passwordinput').focus();
                }, function() {
                    //点击取消后的回调函数
                    $.toast('已取消转让', 'cancel')
                    $('#passwordinput').val('')
                });
            }else{
                $.confirm("你还没有支付密码",'先去设置', function() {
                    //点击确认后的回调函数
                    window.location.href='/wap/accountsetting?userNo='+from;
                }, function() {
                    //点击取消后的回调函数

                });
            }

        });

        // 输入密码后确认
        $(document).on('click', '#confirm', function () {
            var password = $('#passwordinput').val();
            var userNo= $('#transferFrom').val();
            if (!password || password.length < 6) {
                return;
            }

            setPayPwd(userNo);
            flag=valPayPwd(password);
            if(flag){//密码正确
                   flag= transferFinace();
                   if(flag){
                       $.toast('转让成功！');
                       window.location.replace('/wap/mywallet?userNo='+userNo)
                   }else{
                       $.toast('转让失败！',cancel);
                   }
            }else{
                $.toast('支付密码错误！',cancel);
            }

            hidePasswordPop();
        })
        // 输入密码后取消
        $(document).on('click', '#cancel', function () {
            $.toast('您取消了提现', 'cancel')
            hidePasswordPop()
        })
        function hidePasswordPop () {
            $('#password').removeClass('weui-dialog--visible').siblings('.weui-mask-custom').removeClass('weui-mask--visible')
            $('#passwordinput').val('');
        }
    })

    $('#transferTo').on('input propertychange',function () {
        var to = $('#transferTo').val();
        confireUserNo(to);
    })

/*转入账号的确认*/
function  confireUserNo(userNo) {
    $.ajax({
        url:'/wap/selectUserNoByUserNo?userNo='+userNo,
        success:function (res) {
            if(res.userNo!=null){
                $('.msg').html("请确认账号！");
                $('.msg').css("color","blue");
                f=true;
            }else{
                $('.msg').html("账号不存在！");
                f=false;
            }
        }
    })
    return f;
}
    /*确认支付密码*/
    var flag=false;
    function valPayPwd(payPwd) {
        $.ajax({
            async: false,
            type:'post',
            url:'/wap/valPayPwd',
            data:{
                payPwd:payPwd
            },
            success:function (res) {
                flag= res.flag;
            }
        })
        return flag;
    }
function transferFinace(){
    var to = $('#transferTo').val();
    var total = $('#total').val();
    var max = Number($('#max').text())
    $.ajax({
        type:'post',
        url:'/wap/transferFinace',
        data:{
          userNo:to,
          cashCount:total,
          balance:max
        },
        async: false,
        success:function (res) {
            if(res.msg=='1'){
                flag=true;
            }else{
                flag=false;
            }
        }
    })
    return flag;
}
function  setTime(userNo) {
        window.location.href='/wap/mywallet?userNo='+userNo;
}

/*设置支付密码*/
    function  setPayPwd(userNo) {
        $.ajax({
            async: false,
            url:'/wap/selectUserNoByUserNo?userNo='+userNo,
            success:function (res) {
                if(res.userNo.payPwd!=null){
                    flag=true;
                }
            }
        })
        return flag;
    }

</script>
</html>